<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="controller.CommonValues"%>
<%@page import="model.database.DBException"%>
<%@page import="controller.EmployeeController"%>
<%@page import="view.WebCommons"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="customerData" class="view.useBeans.CustomerData" scope="page" />
<jsp:setProperty name="customerData" property="number" />

<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Get customer's orders</title>
</head>
<body>

	<span class="customHeader2">Get orders of a customer</span>

	
<!-- 	get customer's number -->
	
	<div id="page">
		<div class="post">

			<div id="sidebar">
				<ul>
					<li>
						<h2>Please enter customer's number</h2>
					</li>
				</ul>
			</div>
			<div id="content">
				<form METHOD=POST ACTION="GetOrders.jsp">
					<table>
						<tr>
							<td><INPUT TYPE=TEXT NAME=number
								SIZE=<%=WebCommons.NUMBER_SIZE%>></td>
							<td><INPUT TYPE=SUBMIT VALUE="Go!" class="mySubmitButton"></td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</div>

	<%
		// check if already submitted customer's number
		if (!(request.getParameter("number") == null || request
				.getParameter("number").equals(""))) {

			// extract number
			String numberStr = request.getParameter("number");

			// verify numeric format
			boolean doQuery = true; // indicates if should perform quer
						
			// parse numeric value
			Integer customerNumber = customerData.getCustomerNumberValue();
			
			if (customerNumber == null) {

				// cannot get orders
				doQuery = false;
				
				// diplsay error message
				%>
				<span class="notify"><br>Please enter a positive integer for
					customer number</span>
				<%
			}

			// if can, get the orders
			if (doQuery) {

				try {

					// extract controller from session
					EmployeeController controller = (EmployeeController) session
							.getAttribute(WebCommons.CONTROLLER);

					// try getting customer's orders
					List<Map<String, String>> ordersDetails = controller
							.getOrders(customerNumber); // TODO CHECK IF BEST WAY TO VERIFY CUTSOMER							

					// check success
					if (ordersDetails == null) {

						// display error message
						%>
						<span class="notify"><br>Sorry, could not get orders- no
							customer with the number "<%=customerNumber%>"</span>
						<%
					} else { // customer exists, show orders

						// store given customer number in session
						session.setAttribute(WebCommons.CUSTOMER_NUMBER,
								customerNumber);

						// move to page to get the orders
						response.sendRedirect("../customerTransactions/GetOrders.jsp");
					}

				} catch (DBException e) {

					e.printStackTrace(); // TODO DEBUG PRINT!!!

					// move to error page
					response.sendRedirect("../../commonPages/ErrorPage.jsp");
				}
			}
		}

		// return to main menu
		String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
	%>
	<div id="footer">
		<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back
				to Menu</A></span>
	</div>
</body>
</html>